#!/usr/bin/env bash
# -*- encoding UTF-8 -*-
# Author: kjzhu

export PGRDIR=$(cd `dirname $0`; pwd)
export WORKDIR=${PGRDIR}/../../../../
export GRAPDIR=${PGRDIR}/../../chart/Gray
export VERSION=$1
export GRAYTYPE=$2

source ${WORKDIR}/release/docker/env.sh

# only set VERSION if not set
: ${VERSION:="latest"}


# Different environment release names cannot be duplicated in different namespaces of the same cluster
export RELEASE_NAME=${DOCKER_IMAGE_NAME}
export NAMESPACE=${K8S_NAMESPACE}
echo "Release name: ${RELEASE_NAME}-${GRAYTYPE} , Namespace: ${NAMESPACE}"


# 更改Gray模板文件
sed -i "s@template-server@${RELEASE_NAME}@g" $GRAPDIR/Chart.yaml
sed -i "s@prodlabel@${RELEASE_NAME}@g" $GRAPDIR/templates/deploymentstatefulset.yaml
sed -i "s@temple@${RELEASE_NAME}-${GRAYTYPE}@g" $GRAPDIR/templates/virtualservice.yaml
sed -i "s@prodhost@${RELEASE_NAME}@g" $GRAPDIR/templates/virtualservice.yaml

export HELM_OPTS=" \
    --namespace ${NAMESPACE} \
    -f ${PGRDIR}/values.yaml \
    --set namespace=${NAMESPACE} \
    --set image.repository=${DOCKER_REGISTRY_URL}/${DOCKER_IMAGE_NAME} \
    --set image.tag=${VERSION} \
    --set istio=${GRAYTYPE} \
    --set environment.aliyun_logs_${RELEASE_NAME}-${GRAYTYPE}=stdout \
    --timeout 600s \
    --wait
"

helm upgrade --install  --version ${VERSION} ${HELM_OPTS} ${RELEASE_NAME}-${GRAYTYPE}  ${WORKDIR}/release/k8s/chart/Gray

